package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.commons.lang.StringEscapeUtils;

import com.conversationboard.cache.newanswers.NewAnswersThread;
import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.User;

public class XmlNewAnswers {

	static void deliverAsXML(HttpServletResponse response, String contextPath, String stylesheet, int numberOfNewAnswers, List<NewAnswersThread> threads, User user) throws IOException,
			TransformerConfigurationException, TransformerException, SQLException {

		StringBuilder buffer = new StringBuilder();

		buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");

		buffer.append("<RecentlyUpdatedThreads>");

		buffer.append("<URI>");
		buffer.append(contextPath);
		buffer.append("</URI>");

		buffer.append("<TotalMessages>" + threads.size() + "</TotalMessages>");

		if (numberOfNewAnswers > 0) {
			buffer.append("<DisplayingLast>" + numberOfNewAnswers + "</DisplayingLast>");
		}

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		int i = 0;

		for (NewAnswersThread thread : threads) {

			if ((numberOfNewAnswers > 0) && (i++ + numberOfNewAnswers < threads.size())) {
				continue;
			}

			Board board = Boards.getBoard(thread.getBoardId());

			/* If there's no user, display no private or hidden boards */

			if ((user == null) && (board.isPrivateBoard() || board.isHidden())) {
				continue;
			}

			/* If there is a user, then check that the user can read this thread */

			if ((user != null) && (!user.canRead(thread.getBoardId()))) {
				continue;
			}

			String title = StringEscapeUtils.escapeXml(thread.getTitle());

			/* Force the format of dates to a standard form */

			String lastUpdatedString = formatter.format(thread.getLastUpdated());
			String createdOnString = formatter.format(thread.getTimestamp());

			buffer.append("<RecentlyUpdatedThread>");
			buffer.append("<BoardId>" + thread.getBoardId() + "</BoardId>");
			buffer.append("<ThreadId>" + thread.getThreadId() + "</ThreadId>");
			buffer.append("<Title>" + title + "</Title>");
			buffer.append("<CreatedOn>" + createdOnString + "</CreatedOn>");
			buffer.append("<LastUpdated>" + lastUpdatedString + "</LastUpdated>");
			buffer.append("<Deleted>" + thread.isDeleted() + "</Deleted>");
			buffer.append("<Worksafe>" + thread.isWorksafe() + "</Worksafe>");
			buffer.append("<Locked>" + thread.isLocked() + "</Locked>");
			buffer.append("<Poll>" + thread.isPoll() + "</Poll>");
			buffer.append("</RecentlyUpdatedThread>");
		}

		buffer.append("</RecentlyUpdatedThreads>");

		String xmlData = buffer.toString();

		response.setContentType("text/xml");
		response.getWriter().append(xmlData);

	}

}
